package com.android.tea;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

import com.android.tea.data.SettingLoader;

public class TeaFriendTabActivity extends TabActivity {

	protected static final String TAG = "FriendTabActivity";
	private TabHost tabHost;
	private String AllFriends = "allFriends";
	private String Contacts = "contacts";
	private String Chart = "chart";
	private String Select = "select";
	
	private TextView tab_allfriends;
	private TextView tab_contacts;
	private TextView tab_chart;
	private TextView tab_select;
	
	private TranslateAnimation tabBarAnimation;
	private int screenW;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_tab_layout);
		initLayout();
		
		Intent intent = new Intent(this, AllFriendsActivity.class);
		intent.putExtra("mTitle", 0);
		intent.putExtra("mButton", 0);
		TabHost.TabSpec spec1 = tabHost.newTabSpec(AllFriends).setIndicator(AllFriends).setContent(intent);
		tabHost.addTab(spec1);

		Intent contact = new Intent(this, TeaFriendContactsActivity.class);
		TabHost.TabSpec spec2 = tabHost.newTabSpec(Contacts).setIndicator(Contacts).setContent(contact);
		tabHost.addTab(spec2);

		TabHost.TabSpec spec3 = tabHost.newTabSpec(Chart).setIndicator(Chart)
				.setContent(new Intent(this, TeaFriendsChartActivity.class));
		tabHost.addTab(spec3);
		
		TabHost.TabSpec spec4 = tabHost.newTabSpec(Select).setIndicator(Select)
		.setContent(new Intent(this, TeaFriendSelectActivity.class));
		tabHost.addTab(spec4);

//		tab_friends.setTextColor(getResources().getColor(R.color.tab_current));
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				onTabChange(tabId);
			}
		});

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		int index=SettingLoader.getFriendTabIndex(this);
		String tabid=AllFriends;
		if(index==1){
			tabid=Contacts;
		}else if(index==2){
			tabid=Chart;
		}else if(index==3) {
			tabid = Select;
		}
		onTabChange(tabid);
		tabHost.setCurrentTab(index);
	}
	private void initLayout() {
		tabHost = getTabHost();
		Options opts=new Options();
		opts.inJustDecodeBounds=true;
//		BitmapFactory.decodeResource(getResources(), R.drawable.header_friend_tab_bg, opts);
		int tabH=(int) (opts.outHeight*getResources().getDisplayMetrics().density/2);
		tab_allfriends=(TextView) findViewById(R.id.tab_allfriends);
		tab_contacts=(TextView) findViewById(R.id.tab_contact);
		tab_chart=(TextView) findViewById(R.id.tab_chart);
		tab_select = (TextView)findViewById(R.id.tab_select);
		tab_allfriends.setHeight(tabH);
		tab_contacts.setHeight(tabH);
		tab_chart.setHeight(tabH);
		
//		tab_move_bar=(TextView) findViewById(R.id.tab_move_bar);
//		screenW=Utils.getScreenWidth(this);
//		tab_move_bar.setWidth(screenW/3);
		
		OnClickListener tabOnClicklistener=new OnClickListener() {
			@Override
			public void onClick(View v) {
			int id=v.getId();
			switch (id) {
			case R.id.tab_allfriends:
				tabHost.setCurrentTabByTag(AllFriends);
				break;
			case R.id.tab_contact:
				tabHost.setCurrentTabByTag(Contacts);
				break;
			case R.id.tab_chart:
				tabHost.setCurrentTabByTag(Chart);
				break;
			case R.id.tab_select:
				tabHost.setCurrentTabByTag(Select);
				break;
			}
			}
		};
		tab_allfriends.setOnClickListener(tabOnClicklistener);
		tab_contacts.setOnClickListener(tabOnClicklistener);
		tab_chart.setOnClickListener(tabOnClicklistener);
		tab_select.setOnClickListener(tabOnClicklistener);
		
//		tabHost.setBackgroundResource(R.drawable.bitmap);
	}
	
	@Override
	public void onBackPressed() {
		// 退出程序
		Activity parent = this.getParent();
		if(parent!=null) {
			parent.onBackPressed();
		}else{
			super.onBackPressed();
		}
	}

	int startX = 0;
	int endX = 0;

	private void onTabChange(String tabId) {
		int tabIndex = 0;
		tab_allfriends.setTextColor(getResources().getColor(R.color.tab_default));
		tab_contacts.setTextColor(getResources().getColor(R.color.tab_default));
		tab_chart.setTextColor(getResources().getColor(R.color.tab_default));
		tab_select.setTextColor(getResources().getColor(R.color.tab_default));
		
		if(AllFriends.equalsIgnoreCase(tabId)){
			tabIndex=0;
			tab_allfriends.setTextColor(getResources().getColor(R.color.white));
		}else if(Contacts.equalsIgnoreCase(tabId)){
			tabIndex=1;
			tab_contacts.setTextColor(getResources().getColor(R.color.white));
		}else if(Chart.equalsIgnoreCase(tabId)){
			tabIndex=2;
			tab_chart.setTextColor(getResources().getColor(R.color.white));
		}else if(Select.equalsIgnoreCase(tabId)){
			tabIndex=3;
			tab_select.setTextColor(getResources().getColor(R.color.white));
		}
//		int width=tab_friends.getWidth();
//		if(width==0){
//			width=Utils.getScreenHeight(this)/3;
//		}
//		endX = width * (tabIndex);
		
//		tabBarAnimation = new TranslateAnimation(startX, endX, 0, 0);
//		tabBarAnimation.setDuration(200);
//		tabBarAnimation.setFillEnabled(true);
//		tabBarAnimation.setFillAfter(true);
//		tab_move_bar.startAnimation(tabBarAnimation);
		
//		startX=endX;
		
		SettingLoader.setFriendTabIndex(this, tabIndex);
	}

}
